<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;


use \think\Db;
use app\admin\model\UserGoodsNum as UserGoodsNumModel;

class UserGoodsNum extends Permissions
{
    /**
     * 轮播图列表
     * @return [type] [description]
     */
    public function index()
    {
        //实例化轮播图模型
        $model = new UserGoodsNumModel();

        $admin = empty($where) ? $model
            ->join('user','user_goods_num.user_id = user.id')
            ->join('goods','goods.id=user_goods_num.goods_id')
            ->order('user_goods_num.create_time sort')->field('user_goods_num.*,user.username,goods.goods_name')->paginate(20) : $model->where($where)->order('user_goods_num.create_time desc')->paginate(20, false, ['query' => $this->request->param()]);

        $this->assign('admin', $admin);
        return $this->fetch();
    }


    /**
     * 管理员的添加及修改
     * @return [type] [description]
     */
    public function publish()
    {
        //获取菜单id
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $model = new UserGoodsNumModel();
        //是正常添加操作
        if ($id > 0) {
            //是修改操作
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['user_id', 'require', '用户不能为空'],
                    ['goods_id', 'require', '商品不能为空'],
                    ['goods_price', 'require', '代理商拿货价格不能为空'],
                    ['goods_num', 'require', '商品数量不能为空'],
                ]);

                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //验证菜单是否存在
                $user_goods_num = $model->where('id', $id)->find();

                if (empty($user_goods_num)) {
                    return $this->error('id不正确');
                }
                if (false == $model->allowField(true)->save($post, ['id' => $id])) {
                    return $this->error('修改失败');
                } else {
//                    addlog($model->id);//写入日志
                    return $this->success('修改成功', 'admin/user_goods_num/index');
                }
            } else {
                //非提交操作
                $info = $model->where('id', $id)->find();
                if (!empty($info)) {

                    $user = Db::name('user')->select();
                    $goods = Db::name('goods')->field('id,goods_name')->select();
                    //非提交操作
                    $this->assign('user', $user);
                    $this->assign('goods', $goods);
                    $this->assign('info', $info);
                    return $this->fetch();
                } else {
                    return $this->error('id不正确');
                }
            }
        } else {
            //是新增操作
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['user_id', 'require', '用户不能为空'],
                    ['goods_id', 'require', '商品不能为空'],
                    ['goods_price', 'require', '代理商拿货价格不能为空'],
                    ['goods_num', 'require', '商品数量不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                if (false == $model->allowField(true)->save($post)) {
                    return $this->error('添加失败');
                } else {
//                    addlog($model->id);//写入日志
                    return $this->success('添加成功', 'admin/user_goods_num/index');
                }
            } else {
                $user = Db::name('user')->select();
                $goods = Db::name('goods')->field('id,goods_name')->select();
                //非提交操作
                $this->assign('user', $user);
                $this->assign('goods', $goods);
                return $this->fetch();
            }
        }

    }

    public function delete()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if (false == Db::name('user_goods_num')->where('id', $id)->delete()) {
                return $this->error('删除失败');
            } else {
//                addlog($id);//写入日志
                return $this->success('删除成功', 'admin/user_goods_num/index');
            }

        }
    }

    public function status()
    {
        if($this->request->isPost()){
            $post = $this->request->post();
            if(false == Db::name('user_goods_num')->where('id',$post['id'])->update(['status'=>$post['status']])) {
                return $this->error('设置失败');
            } else {
//                addlog($post['id']);//写入日志
                return $this->success('设置成功','admin/user_goods_num/index');
            }
        }
    }
}
